package com.ruoyi.project.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.domain.RiskType;
import com.ruoyi.project.service.IRiskTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 风险类别Controller
 * 
 * @author zhaogang
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/project/riskType")
public class RiskTypeController extends BaseController
{
    @Autowired
    private IRiskTypeService riskTypeService;

    /**
     * 查询风险类别列表
     */
    @PreAuthorize("@ss.hasPermi('project:riskType:list')")
    @GetMapping("/list")
    public TableDataInfo list(RiskType riskType)
    {
        startPage();
        List<RiskType> list = riskTypeService.selectRiskTypeList(riskType);
        return getDataTable(list);
    }

    /**
     * 导出风险类别列表
     */
    @PreAuthorize("@ss.hasPermi('project:riskType:export')")
    @Log(title = "风险类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RiskType riskType)
    {
        List<RiskType> list = riskTypeService.selectRiskTypeList(riskType);
        ExcelUtil<RiskType> util = new ExcelUtil<RiskType>(RiskType.class);
        util.exportExcel(response, list, "风险类别数据");
    }

    /**
     * 获取风险类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:riskType:query')")
    @GetMapping(value = "/{risktypeId}")
    public AjaxResult getInfo(@PathVariable("risktypeId") Long risktypeId)
    {
        return success(riskTypeService.selectRiskTypeByRisktypeId(risktypeId));
    }

    /**
     * 新增风险类别
     */
    @PreAuthorize("@ss.hasPermi('project:riskType:add')")
    @Log(title = "风险类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody RiskType riskType)
    {
        String count = riskTypeService.Addjudgment(riskType);
        if (count  != null) {
            return AjaxResult.error("风险类别名称重复");

        }else {
            return toAjax(riskTypeService.insertRiskType(riskType));
        }

    }

    /**
     * 修改风险类别
     */
    @PreAuthorize("@ss.hasPermi('project:riskType:edit')")
    @Log(title = "风险类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody RiskType riskType)
    {
        String count = riskTypeService.Addjudgment(riskType);
        if (count  != null) {
            return AjaxResult.error("风险类别名称重复");

        }else {
            return toAjax(riskTypeService.updateRiskType(riskType));
        }


    }

    /**
     * 删除风险类别
     */
    @PreAuthorize("@ss.hasPermi('project:riskType:remove')")
    @Log(title = "风险类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{risktypeIds}")
    public AjaxResult remove(@PathVariable Long[] risktypeIds)
    {
        for(Long id:risktypeIds){
            Long judgements  = riskTypeService.riskTypeDelete(id);
            if(judgements != null){
                return AjaxResult.error("此风险类别不能删除，因为在项目风险中已使用");
            }
            riskTypeService.deleteRiskTypeByRisktypeIds( new Long[]{id});

        }
        return AjaxResult.success("删除成功");
    }
}
